package com.kedacom.config.factory;

import java.io.IOException;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.auth.AuthInfo;
import net.rubyeye.xmemcached.command.BinaryCommandFactory;
import net.rubyeye.xmemcached.utils.AddrUtil;

import org.springframework.beans.factory.FactoryBean;

import com.kedacom.config.client.EmailClient;
import com.kedacom.config.configuration.EmailConfiguration;
import com.kedacom.config.configuration.MemcachedConfiguration;
import com.kedacom.config.resource.DefaultConfigurationResource;

@SuppressWarnings("unchecked")
public class ConfigFactoryBean implements FactoryBean {

	private EmailClient emailClient;
	private MemcachedClient memcachedClient;
	private Class clazz = null;
	private String propertiesFile;

	public String getPropertiesFile() {
		return propertiesFile;
	}

	public void setPropertiesFile(String propertiesFile) {
		this.propertiesFile = propertiesFile;
	}

	@Override
	public Object getObject() throws Exception {
		if (EmailClient.class.equals(clazz)) {
			return emailClient;
		} else if (MemcachedClient.class.equals(clazz)) {
			return memcachedClient;
		}
		
		return "";
	}

	@Override
	public Class getObjectType() {
		return clazz;
	}
	
	public void createEmailClient() {
		//1. FactoryBean 类型
		clazz = EmailClient.class;
		
		//2. 初始化并加载Configuration
		EmailConfiguration emailConfiguration = new EmailConfiguration();
		DefaultConfigurationResource resource = new DefaultConfigurationResource(propertiesFile);
		try {
			emailConfiguration.loadConfiguration(resource);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		
		//3. 封装emailClient
		emailClient = new EmailClient();
		emailClient.setEmailConfiguration(emailConfiguration);
	}
	
	public void createMemcachedClient() {
		//1. FactoryBean 类型
		clazz = MemcachedClient.class;
		
		//2. 初始化并加载Configuration
		MemcachedConfiguration memcachedConfiguration = new MemcachedConfiguration();
		DefaultConfigurationResource resource = new DefaultConfigurationResource(propertiesFile);
		try {
			memcachedConfiguration.loadConfiguration(resource);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		
		//3. 封装memcachedClient
		MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil
				.getAddresses(memcachedConfiguration.getMemcachedHost() + ":" + memcachedConfiguration.getMemcachedPort()));
		builder.addAuthInfo(AddrUtil.getOneAddress(memcachedConfiguration.getMemcachedHost() + ":" + memcachedConfiguration.getMemcachedPort()),  
                AuthInfo.typical(memcachedConfiguration.getMemcachedUsername(), memcachedConfiguration.getMemcachedPassword()));
		builder.setCommandFactory(new BinaryCommandFactory());  
		try {
			memcachedClient = builder.build();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

}
